package com.chhuang.ewcrm.controller;

import com.chhuang.ewcrm.bean.QueryBean;
import com.chhuang.ewcrm.consts.EwcrmConstants;
import com.chhuang.ewcrm.entity.EBuyGoods;
import com.chhuang.ewcrm.service.EBuyGoodsService;
import com.chhuang.ewcrm.vo.ResultBean;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * (EBuyGoods)表控制层
 *
 * @author chhuang
 * @since 2020-11-14 11:24:07
 */
@RestController
@RequestMapping("/service/eBuyGoods")
public class EBuyGoodsController {
    Logger logger = LoggerFactory.getLogger(EBuyGoodsController.class);
    /**
     * 服务对象
     */
    @Resource
    private EBuyGoodsService eBuyGoodsService;

    /**
     * 获取所有原料
     *
     * @return 单条数据
     */
    @GetMapping("/getAll")
    public ResultBean<List<EBuyGoods>> getAll() {
        ResultBean<List<EBuyGoods>> resultBean = new ResultBean<>();
        resultBean.setSuccess(true);
        resultBean.setData(this.eBuyGoodsService.query(null));
        return resultBean;
    }

    /**
     * 删除原料
     * @param goodsId
     * @return
     */
    @DeleteMapping("/removeOne/{goodsId}")
    public ResultBean<?> removeOne(@PathVariable("goodsId") String goodsId){
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.setSuccess(true);
        if(StringUtils.isNotEmpty(goodsId)){
            //删除原料
            resultBean.setSuccess(this.eBuyGoodsService.deleteById(goodsId));
        }else{
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 根据id列表批量删除
     * @param goodsIds
     * @return
     */
    @PutMapping("/removeList")
    public ResultBean<?> removeList(@RequestBody List<String> goodsIds){
        ResultBean<?> resultBean = new ResultBean<>();
        if(goodsIds!=null && !goodsIds.isEmpty()){
            resultBean.setSuccess(this.eBuyGoodsService.deleteByIds(goodsIds)>0);
        }else{
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 修改原料信息
     * @param eBuyGoods
     * @return
     * @throws IOException
     */
    @PutMapping("/editOne")
    public ResultBean<EBuyGoods> editOne(@RequestBody EBuyGoods eBuyGoods){
        ResultBean<EBuyGoods> resultBean = new ResultBean<EBuyGoods>();
        resultBean.setSuccess(false);
        if(eBuyGoods!=null && StringUtils.isNotEmpty(eBuyGoods.getGoodsName())) {
            if (StringUtils.isEmpty(eBuyGoods.getGoodsId())) { //添加
                Date now = new Date();
                eBuyGoods.setCreateTime(now);
                eBuyGoods.setModifyTime(now);
                eBuyGoods.setBuyTotalWeight(0d);
                eBuyGoods.setBuyTotalAmount(0d);
                eBuyGoods.setBuyAveragePrice(0d);
                eBuyGoods = this.eBuyGoodsService.insert(eBuyGoods);
                resultBean.setData(eBuyGoods);
                resultBean.setSuccess(resultBean.getData()!=null);
            } else{ //修改
                EBuyGoods buyGoods = eBuyGoodsService.queryById(eBuyGoods.getGoodsId());
                if (buyGoods != null) {
                    //修改原料信息
                    buyGoods.setGoodsName(eBuyGoods.getGoodsName());
                    buyGoods.setWarehouseId(eBuyGoods.getWarehouseId());
                    buyGoods.setWarehouseName(eBuyGoods.getWarehouseName());
                    buyGoods.setModifyTime(new Date());
                    eBuyGoods = this.eBuyGoodsService.update(buyGoods);
                    resultBean.setData(eBuyGoods);
                    resultBean.setSuccess(resultBean.getData()!=null);
                } else
                    resultBean.setMessage("没有找到原料");
            }
        } else
            resultBean.setMessage("参数不能为空");
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param goodsId 主键
     * @return 单条数据
     */
    @GetMapping("/getOne")
    public ResultBean<EBuyGoods> getOne(String goodsId) {
        ResultBean<EBuyGoods> resultBean = new ResultBean<>();
        resultBean.setSuccess(false);
        if(StringUtils.isNotEmpty(goodsId)){
            resultBean.setSuccess(true);
            resultBean.setData(this.eBuyGoodsService.queryById(goodsId));
        }else{
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 表格查询
     * @param queryBean
     * @return
     */
    @PostMapping("/queryPage")
    public ResultBean<PageInfo<EBuyGoods>> query(@RequestBody QueryBean<EBuyGoods> queryBean){
        logger.debug(queryBean.toString());
        ResultBean<PageInfo<EBuyGoods>> resultBean = new ResultBean<>();

        PageInfo<EBuyGoods> pageInfo = null;
        List<EBuyGoods> buyGoodss = null;
        if(queryBean!=null) {
            if (StringUtils.isNotEmpty(queryBean.getOrderBy()))
                pageInfo = eBuyGoodsService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize(), queryBean.getOrderBy());
            else
                pageInfo = eBuyGoodsService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize());
            resultBean.setData(pageInfo);
            resultBean.setSuccess(true);
            resultBean.setCode(queryBean.getDraw());
        }else {
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }

        logger.debug(resultBean.toString());
        return resultBean;
    }
}